package cn.tedu.csmall.passport.web;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data
@Accessors(chain = true)// 该注解可以实现【方法2】那样的setter方法，不影响@Data注解的getter，setter方法，只是多了一种选择
@JsonInclude(JsonInclude.Include.NON_NULL)// 响应结果中不为NULL才显示
public class JsonResult implements Serializable {

    private Integer state;
    private String message;
    private Object data;

    /**
     * 通用返回成功(不带业务数据)
     * @return
     */
    public static JsonResult success() {
        return new JsonResult()
                .setState(ServiceCode.OK.getState())
                .setMessage(ServiceCode.OK.getMessage());
    }

    /**
     * 通用返回成功(带业务数据)
     * @param data 业务数据
     * @return
     */
    public static JsonResult success(Object data) {
        return new JsonResult()
                .setState(ServiceCode.OK.getState())
                .setMessage(ServiceCode.OK.getMessage())
                .setData(data);
    }

    /**
     * 错误结果
     * @param serviceCode 结果状态码枚举
     * @return
     */
    public static JsonResult error(ServiceCode serviceCode) {
        return new JsonResult()
                .setState(serviceCode.getState())
                .setMessage(serviceCode.getMessage());
    }

    /**
     * 错误结果
     * @param serviceCode 结果状态码枚举
     * @return
     */
    public static JsonResult error(ServiceCode serviceCode, String msg) {
        return new JsonResult()
                .setState(serviceCode.getState())
                .setMessage(msg);
    }
}